#include <stdio.h>
#include <stdlib.h>	/* exit() */
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
#include <time.h>
#include <limits.h>
#include <errno.h>
#include <signal.h>
#include <setjmp.h>


/*
 * longjmp 对各类变量的影响
 */

#define TOK_ADD	5

jmp_buf jmpbuffer;
static int globval;

static void f1(int, int, int, int);
static void f2(void);

int main(void)
{
	int autoval;
	register int regival;
	volatile int volaval;
	static int   statval;

	globval = 1;
	autoval = 2;
	regival = 3;
	volaval = 4;
	statval = 5;

	if (setjmp(jmpbuffer) != 0) {
		printf("after longjmp:\n");
		printf("globval = %d, autoval = %d, regival = %d, "
				"volaval = %d, statuval = %d\n", 
					globval, autoval, regival, volaval, statval);

		exit(0);
	}

	/*
	 * change variables after setjmp, but before longjmp.
	 */
	globval = 95;
	autoval = 96;
	regival = 97;
	volaval = 98;
	statval = 99;

	f1(autoval, regival, volaval, statval);	/* never returns */

	exit(0);
}

static void f1(int i, int j, int k, int l)
{
	printf("in f1():\n");
	printf("globval = %d, autoval = %d, regival = %d, "
				"volaval = %d, statuval = %d\n", 
					i, j, k, l);
	
	f2();
}

static void f2(void)
{
	longjmp(jmpbuffer, 1);
}

